﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommonLibrary;
using Oracle.DataAccess.Client;
using Oracle.DataAccess;
using System.Data.OleDb;
using System.Collections;

namespace BSC_OTC_Management.Forms
{
    public partial class FrmPhanBoLenh : Form
    {
        public FrmPhanBoLenh()
        {
            InitializeComponent();
        }

        #region KHAI BAO THAM SO, BIEN CHUNG
        public Code.StaticVariable _code = new Code.StaticVariable();
        string v_CompanyName, v_FundName, v_SubFunName, v_Custodycd, v_Side, V_Symbol, V_LimitQuantity, v_LimitPrice;
        Hashtable v_HtbCompanyLIst = new Hashtable();
        Hashtable v_HtbSide = new Hashtable();
        DataTable dtb_tep2;
        DataTable dtb_tep;
        public DataTable v_dtbCompanyInFoFull;
        public DataTable v_dtbCompanyInFoFind;
        public DataTable v_dtbSide;
        #endregion

        #region CAC HAM TRONG FORM
        private void FILL_HashTable(Hashtable v_HashTable, DataTable v_DataTable, ComboBox v_CbbName)
        {
            try
            {
                v_CbbName.Items.Clear();
                v_HashTable.Clear();
                v_HashTable.Add("ALL", "Tất cả");
                v_CbbName.Items.Add("Tất cả");
                v_CbbName.Text = "Tất cả";
                foreach (DataRow row in v_DataTable.Rows)
                {
                    v_HashTable.Add(row["KeyName"].ToString(), row["KeyValue"].ToString());
                    v_CbbName.Items.Add(row["KeyValue"].ToString());
                }
            }
            catch
            { }
        }

        public string Get_key_HashTable(Hashtable v_HashTable, String v_InValues)
        {
            try
            {
                foreach (DictionaryEntry item in v_HashTable)
                {
                    if (item.Value.ToString() == v_InValues)
                    {
                        return item.Key.ToString();
                    }
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static DataTable EXECUTE_PROCEDURE_ORACLE(string Proce_Name, string PV_COMPANYID, string PV_SIDE, string PV_SYMBOL, double PV_PRICE, double PV_QTY)
        {
            DataSet v_ds;
            try
            {
                ReportParameters[] v_arrRptPara = new ReportParameters[5];
                ReportParameters v_objRptParam;

                v_objRptParam = new ReportParameters();

                v_objRptParam.ParamName = "PV_COMPANYID";
                v_objRptParam.ParamType = "VARCHAR2";
                v_objRptParam.ParamSize = 30;
                v_objRptParam.ParamValue = PV_COMPANYID;
                v_arrRptPara[0] = v_objRptParam;

                v_objRptParam = new ReportParameters();
                v_objRptParam.ParamName = "PV_SIDE";
                v_objRptParam.ParamType = "VARCHAR2";
                v_objRptParam.ParamSize = 30;
                v_objRptParam.ParamValue = PV_SIDE;
                v_arrRptPara[1] = v_objRptParam;

                v_objRptParam = new ReportParameters();
                v_objRptParam.ParamName = "PV_SYMBOL";
                v_objRptParam.ParamType = "VARCHAR2";
                v_objRptParam.ParamSize = 30;
                v_objRptParam.ParamValue = PV_SYMBOL.Trim();
                v_arrRptPara[2] = v_objRptParam;

                v_objRptParam = new ReportParameters();
                v_objRptParam.ParamName = "PV_PRICE";
                v_objRptParam.ParamType = "DOUBLE";
                v_objRptParam.ParamSize = 30;
                v_objRptParam.ParamValue = PV_PRICE;
                v_arrRptPara[3] = v_objRptParam;

                v_objRptParam = new ReportParameters();
                v_objRptParam.ParamName = "PV_QTY";
                v_objRptParam.ParamType = "DOUBLE";
                v_objRptParam.ParamSize = 30;
                v_objRptParam.ParamValue = PV_QTY;
                v_arrRptPara[4] = v_objRptParam;

                DataAccess v_obj;
                v_obj = new DataAccess();
                v_obj.NewDBInstance("@DIRECT_HOST");
                v_ds = new DataSet();
                v_ds.Tables.Clear();
                v_ds = v_obj.ExecuteStoredReturnDataset(Proce_Name, v_arrRptPara);
                return v_ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), " EXECUTE_PROCEDURE_ORACLE : " + Proce_Name);
                return null;
            }
        }

        public static DataTable EXECUTE_CMD_ORACLE(string cmd_SQL)
        {
            DataSet v_ds;
            try
            {
                DataAccess v_obj;
                v_obj = new DataAccess();
                v_obj.NewDBInstance("@DIRECT_HOST");
                v_ds = new DataSet();
                v_ds.Tables.Clear();
                v_ds = v_obj.ExecuteSQLReturnDataset(CommandType.Text, cmd_SQL);
                return v_ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), " EXECUTE_CMD_ORACLE : " + cmd_SQL);
                return null;
            }
        }

        public void CALCULATE_ALOCATE_ORDER()
        {
            string v_company = Get_key_HashTable(v_HtbCompanyLIst, this.cbb_Company_Name.Text);
            string v_Side = Get_key_HashTable(v_HtbSide, this.Cbb_MH1_Side_Loai_lenh.Text);

            if (v_company == "ALL")
            {
                MessageBox.Show("Bạn chưa chọn công ty quản lý quỹ", "Thông báo");
                this.cbb_Company_Name.Focus();
                return;
            }

            if (v_Side == "ALL")
            {
                MessageBox.Show("Bạn chưa chọn loại lệnh Mua/Bán ", "Thông báo");
                this.Cbb_MH1_Side_Loai_lenh.Focus();
                return;
            }

            string v_Symbol = this.cbb_MH1_Symbol_in.Text;
            Double v_Price, V_QTTY;
            try
            {
                v_Price = double.Parse(this.cbb_MH1_gia_dat_du_kien.Text);

            }
            catch
            {
                v_Price = 0;

            }

            try
            {
                V_QTTY = double.Parse(this.cbb_MH1_KL_Du_kien.Text);
            }
            catch
            {

                V_QTTY = 0;
            }

            dtb_tep = new DataTable();
            dtb_tep2 = new DataTable();
            dtb_tep = EXECUTE_PROCEDURE_ORACLE("FAO002", v_company, v_Side, v_Symbol, v_Price, V_QTTY);
            dtb_tep2 = EXECUTE_PROCEDURE_ORACLE("FAO003", v_company, v_Side, v_Symbol, v_Price, V_QTTY);

            if (this.radioButton_PhanBo_Lenh.Checked)
            {
                this.dataGridView2_lenh_Phan_bo.DataSource = null;
                this.dataGridView2_lenh_Phan_bo.DataSource = dtb_tep;
            }
            else
            {
                this.dataGridView2_lenh_Phan_bo.DataSource = null;
                this.dataGridView2_lenh_Phan_bo.DataSource = EXECUTE_PROCEDURE_ORACLE("FAO003", v_company, v_Side, v_Symbol, v_Price, V_QTTY); ;
            }

        }

        public void REFRESH_ORG_ORDER()
        {
            try
            {
                string v_company = Get_key_HashTable(v_HtbCompanyLIst, this.cbb_Company_Name.Text);
                string v_Side = Get_key_HashTable(v_HtbSide, this.Cbb_MH1_Side_Loai_lenh.Text);
                string v_Symbol = this.cbb_MH1_Symbol_in.Text;
                this.dataGridView1.DataSource = EXECUTE_PROCEDURE_ORACLE("FAO001", v_company, v_Side, v_Symbol, 0, 0);
            }
            catch
            {

            }
        }
        void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex % 2 == 0)
            {
                e.CellStyle.BackColor = Color.FromArgb(195, 231, 195);
            }
            else
            {
                e.CellStyle.BackColor = Color.FromArgb(253, 240, 208);
            }
        }

        #endregion

        #region CAC SU KIEN TREN CONTROLS

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvResult_CellFormatting);

            v_dtbCompanyInFoFull = EXECUTE_CMD_ORACLE("Select * From bsc_vw_fun_info");
            v_dtbCompanyInFoFind = EXECUTE_CMD_ORACLE("Select companyid KeyName , name KeyValue From bsc_fao_company_info");
            v_dtbSide = EXECUTE_CMD_ORACLE("Select cdval KeyName, cdcontent KeyValue From ALLCODE where cdname='BORS' and cdtype='OD'");
            FILL_HashTable(v_HtbCompanyLIst, v_dtbCompanyInFoFind, this.cbb_Company_Name);
            FILL_HashTable(v_HtbSide, v_dtbSide, this.Cbb_MH1_Side_Loai_lenh);
        }

        private void btn_Lam_Moi_Click(object sender, EventArgs e)
        {
            REFRESH_ORG_ORDER();
        }

        private void cbb_Company_Name_TextChanged(object sender, EventArgs e)
        {
            REFRESH_ORG_ORDER();
        }

        private void Cbb_MH1_Side_Loai_lenh_TextChanged(object sender, EventArgs e)
        {
            REFRESH_ORG_ORDER();
        }

        private void cbb_MH1_Symbol_in_TextChanged(object sender, EventArgs e)
        {
            REFRESH_ORG_ORDER();
        }

        private void cbb_MH1_gia_dat_du_kien_TextChanged(object sender, EventArgs e)
        {
            CALCULATE_ALOCATE_ORDER();
        }

        private void cbb_MH1_KL_Du_kien_TextChanged(object sender, EventArgs e)
        {
            CALCULATE_ALOCATE_ORDER();
        }

        private void radioButton_PhanBo_Lenh_CheckedChanged(object sender, EventArgs e)
        {
            CALCULATE_ALOCATE_ORDER();
        }

        private void btn_DatlenhvaoFlex_Click(object sender, EventArgs e)
        {
            try
            {

                string v_strLoaiLenh = cbb_Loai_lenh.Text;
                int v_nPrice, v_nSumQTTY0;

                try
                {
                    v_nPrice = Int32.Parse(this.cbb_MH1_gia_dat_du_kien.Text);
                    if (v_nPrice <= 0)
                    {
                        MessageBox.Show("Giá nhập không hợp lệ", "Thông báo");
                        return;
                    }

                }
                catch
                {
                    MessageBox.Show("Giá nhập không hợp lệ", "Thông báo");
                    this.cbb_MH1_gia_dat_du_kien.Focus();
                    return;
                };


                try
                {
                    v_nSumQTTY0 = Int32.Parse(this.cbb_MH1_KL_Du_kien.Text);
                    if (v_nSumQTTY0 <= 0)
                    {
                        MessageBox.Show("Khối lượng không hợp lệ", "Thông báo");
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Khối lượng không hợp lệ", "Thông báo");
                    this.cbb_MH1_KL_Du_kien.Focus();
                    return;
                };

                if (v_strLoaiLenh.Trim() == "")
                {
                    MessageBox.Show("Bạn chưa chọn loại lệnh", "Thông báo");
                    this.cbb_Loai_lenh.Focus();
                    return;
                }

                if (dtb_tep2 == null)
                {
                    MessageBox.Show("Không tìm thấy thông tin lệnh đặt", "Thông báo");
                    return;
                }
                else if (dtb_tep2.Rows.Count == 0)
                {
                    MessageBox.Show("Không tìm thấy thông tin lệnh đặt", "Thông báo");
                    return;
                }

                Frm_PlaceOrder frm_PLaceOD = new Frm_PlaceOrder();
                frm_PLaceOD.Get_LenhFlex = dtb_tep2;
                frm_PLaceOD.Get_loaiLenh = cbb_Loai_lenh.Text;
                frm_PLaceOD.ShowDialog();
                this.cbb_Loai_lenh.Text = "";

            }
            catch
            {

            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            // Ignore if a column or row header is clicked
            v_SubFunName = "";
            try
            {
                DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
                this.dataGridView1.CurrentCell = clickedCell;
                int v_CurrentRow = clickedCell.RowIndex;

                v_CompanyName = dataGridView1.Rows[v_CurrentRow].Cells["COMPANYNAME"].Value.ToString();
                v_FundName = dataGridView1.Rows[v_CurrentRow].Cells["FUNDNAME"].Value.ToString();
                v_SubFunName = dataGridView1.Rows[v_CurrentRow].Cells["SUBFUNDNAME"].Value.ToString();
                v_Custodycd = dataGridView1.Rows[v_CurrentRow].Cells["CUSTODYCD"].Value.ToString();
                v_Side = dataGridView1.Rows[v_CurrentRow].Cells["SIDE"].Value.ToString();
                V_Symbol = dataGridView1.Rows[v_CurrentRow].Cells["SYMBOL"].Value.ToString();
                V_LimitQuantity = dataGridView1.Rows[v_CurrentRow].Cells["LIMITQTY"].Value.ToString();
                v_LimitPrice = dataGridView1.Rows[v_CurrentRow].Cells["LIMITPRICE"].Value.ToString();

                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        var relativeMousePosition = dataGridView1.PointToClient(Cursor.Position);
                        this.contextMenuStrip1.Show(dataGridView1, relativeMousePosition);
                    }
                }

            }
            catch
            { }


        }

        private void btn_Them_moi_Click(object sender, EventArgs e)
        {
            try
            {
                Frm_Orgorder_manage frm_orgOrder = new Frm_Orgorder_manage();
                frm_orgOrder.Get_FormType = "ADD";
                frm_orgOrder.Get_dtbCompanyInfo = v_dtbCompanyInFoFull;
                frm_orgOrder.ShowDialog();
            }
            catch { }
        }

        private void toolStripMenuItem1_Them_moi_lenh_chi_thi_Click(object sender, EventArgs e)
        {
            try
            {
                Frm_Orgorder_manage frm_orgOrder = new Frm_Orgorder_manage();
                frm_orgOrder.Get_FormType = "ADD";
                frm_orgOrder.Get_dtbCompanyInfo = v_dtbCompanyInFoFull;
                frm_orgOrder.ShowDialog();
            }
            catch { }

        }

        private void toolStripMenuItem1_Sua_lenh_chi_thi_Click(object sender, EventArgs e)
        {
            try
            {
                if (v_SubFunName.Trim() == "")
                {
                    MessageBox.Show("Chọn lệnh chỉ thị cần sửa", "Thông báo");
                    return;
                }
                Frm_Orgorder_manage frm_orgOrder = new Frm_Orgorder_manage();
                frm_orgOrder.Get_dtbCompanyInfo = v_dtbCompanyInFoFull;
                frm_orgOrder.Get_FormType = "EDIT";
                frm_orgOrder.Get_CompanyName = v_CompanyName;
                frm_orgOrder.Get_FundName = v_FundName;
                frm_orgOrder.Get_SubFunName = v_SubFunName;
                frm_orgOrder.Get_Custodycd = v_Custodycd;
                frm_orgOrder.Get_Side = v_Side;
                frm_orgOrder.Get_Symbol = V_Symbol;
                frm_orgOrder.Get_LimitQuantity = V_LimitQuantity;
                frm_orgOrder.Get_LimitPrice = v_LimitPrice;
                frm_orgOrder.ShowDialog();
            }
            catch { }
        }
        #endregion
    }
}
